###### Title  ##########################################################
## Candidate Qualifications and Out-Group Support:
## Evidence from Afghanistan by Jasmine Bhatia and
## Steve L. Monroe.
## Replication Command File for Conjoint Analysis 
## October 2024

# This file contains analysis and tables of the conjoint survey experiment
# presented in the main article 


## Table of Contents ########
## 1. Upload Data
## 2. Qualifications and Support for Female Candidates (Figures 1, 2, 3, 4)
## 3. Qualifications and Support for Hazara Candidates (Figures 5, 6, 7, 8)
## 4. Qualifications and Support for Female Hazara Candidates (Figures 9, 10, 11)

## 1. Upload Data, Install Packages #####

# load necessary packages

library(cjoint)
library(dplyr)
library(tidyr)
library(tidyverse)
library(survey)
library(Rcpp)
library(estimatr)
library(knitr)
library(here)
library(cli)
library(devtools)
library("xtable")
library("base")
library(cregg)


# set working directory and load dataset

here()

data <- read.csv("conjoint_clean.csv")


# make every variable a factor for "cj" to work, 
# except for the choice and rating variable


data[, -which(names(data) %in% c("Choice", "Rating"))] <- 
  lapply(data[, -which(names(data) %in% c("Choice", "Rating"))], as.factor)


## 2. Qualifications and Men's Support for Female Candidates ####

# subset data to male respondents

male <- data[data$Res_Gender == "Male Respondent",]


# motivate analysis to look at gender and candidate support without qualifications (Figure 1)
gender <- cj(male, Choice ~ Gender,
         id = ~ID,
         estimate= "mm")

group <- c("Female Candidate", "Male Candidate")


gender <- cbind(gender, group)

# Create Figure 1.

figure1 <- ggplot(data = as.data.frame(gender),
            aes(x = estimate,
            y = group)) + 
            geom_point(size = 3)+ 
            geom_pointrange(aes(xmin= estimate - 2*std.error,
             xmax=estimate + 2*std.error))+
            labs(x="Marginal Means", y = "")+
            geom_vline(xintercept = 0.5, 
             linetype = "dashed")


# export to overleaf
#ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/motivation_1.pdf",
 #      width = 20, height = 13, units = "cm")


## Examine how qualifications influence men's support for women



# Create marginal means plot (difference in support among qualified)
# The impact of qualifications across genders

male$high_educated_reorder <- relevel(male$high_educated, ref = "Low")


## F-Test

# F-Test results  with choice
f.test.figure2 <- cj_anova(male, Choice  ~ Gender,
                       by = ~ high_educated
)



gender <- cj(male, Choice ~ Gender,
           id = ~ ID, estimate = "mm_differences",
           by = ~ high_educated_reorder,
           level_order = "ascending") # high educated female > low educaated female


group <- c("Female Candidate", "Male Candidate")

gender  <- gender %>%
  mutate(group = fct_relevel(group, "Female Candidate",
                             "Male Candidate"))


figure2 <- ggplot(data = as.data.frame(gender),
                  aes(x = estimate,
                      y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Mean Differences (High - Low Qualifications)", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")


figure2

# export

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1a2_female.pdf",
 #      width = 20, height = 13, units = "cm")


## Examine difference in gender support across high and low qualified candidates


gender <- cj(male, Choice ~ high_educated_reorder,
          id = ~ ID, estimate = "mm_differences",
          by = ~ Gender,
          level_order = "ascending") 


group <- c("Low Qualified Candidate", "High Qualified Candidate")

gender  <- gender  %>%
  mutate(group = fct_relevel(group, 
                             "Low Qualified Candidate",
                             "High Qualified Candidate"))


figure3 <- ggplot(data = as.data.frame(gender),
                   aes(x = estimate,
                       y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Mean Differences (Male - Female Candidates)", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")


figure3

# export figure

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1a3_female.pdf",
   #    width = 20, height = 13, units = "cm")


# Look at MMs estimatations across gender and qualifications

gender <- cj(male, Choice ~ Education_Gender_Leader,
          id = ~ID,
          estimate= "mm")

group <- c("Female Candidate (Higher Educated)", "Male Candidate (Higher Educated)",
           "Female Candidate (Less Educated)", "Male Candidate (Less Educated)")

gender  <- gender %>%
  mutate(group = fct_relevel(group, "Male Candidate (Higher Educated)",
                             "Female Candidate (Less Educated)",
                             "Male Candidate (Less Educated)",
                             "Female Candidate (Higher Educated)"))



figure4 <- ggplot(data = as.data.frame(gender),
                  aes(x = estimate,
                      y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")+
  ggtitle("")



ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1a_female.pdf",
       width = 20, height = 13, units = "cm")



## Do F-Test

# F-Test results  with choice
f.test.gender <- cj_anova(male, Choice  ~ Gender,
                       by = ~ high_educated
)



## Rating

gender_rating <- cj(male, Rating ~ Education_Gender_Leader,
         id = ~ID,
         estimate= "mm")



# F-Test results with rating
f.test.gender_rating <- cj_anova(male, Rating  ~ Gender,
                      by = ~ high_educated
)



## Power Analysis

# Use cjpowR app
devtools::install_github("m-freitag/cjpowR")
library(cjpowR)

# Calculate the Average Marginal Conditional Effect (AMCE) for
# education and male leadership attribute. 

amces_choice <- cj(male, Choice ~ high_educated_reorder,
                   id = ~ID,
                   estimate= "amce",
                   by = ~ Gender)

# we find that this increases support for women by 14.5 percentage points
# (high qualified - low qualified).


# Use cjpowr package
# delta3 (amces) is 0.145. N is 6792. Treat probability
# is 0.25. Four treatment conditions, each likely to be had.

cjpowr_amcie(
  delta1 = 0,
  delta3 = 0.14,
  alpha = 0.05,
  n = 6792,
  levels1 = 2,
  levels2 = 2,
  p00 = 0.25,
  p01 = 0.25,
  p10 = 0.25,
  p11 = 0.25,
  sims = 1e+05,
  delta0 = 0.5
)



# power estimate of 0.999


cjpowr_amcie(
  delta1 = 0,
  delta3 = 0.14,
  alpha = 0.05,
  power = 0.8,
  levels1 = 2,
  levels2 = 2,
  p00 = 0.25,
  p01 = 0.25,
  p10 = 0.25,
  p11 = 0.25,
  sims = 1e+05,
  delta0 = 0.5
)

# For a power of 0.8, need 1523 observations,
# below our n of 6792



### 3. Qualifications and Non-Hazara Support for Hazara Candidates (Figure 5) ####


nonhazara <- data[data$Res_Hazara == 0, ]


ethnicity <- cj(nonhazara, Choice ~ Hazara,
         id = ~ID,
         estimate= "mm")

group <- c("Hazara Candidate", "Non-Hazara Candidate")


ethnicity <- cbind(ethnicity, group)



figure5 <- ggplot(data = as.data.frame(ethnicity),
                 aes(x = estimate,
                     y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")


# export to overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/motivation_3.pdf",
       width = 20, height = 13, units = "cm")



## How does education influence out-group support for Hazaras?

# re-order education variable and look at differences in means

nonhazara$high_educated_reorder <- relevel(nonhazara$high_educated, ref = "Low")


# F-test

ethnicity <- cj(nonhazara, Choice ~ Education_Hazara_Leader,
          id = ~ID,
          estimate= "mm")


# F-Test results with choice
f.test.ethnicity <- cj_anova(nonhazara, Choice  ~ Hazara,
                       by = ~ Education_Hazara_Leader
)


ethnicity <- cj(nonhazara, Choice ~ Hazara,
           id = ~ ID, estimate = "mm_differences",
           by = ~ high_educated_reorder,
           level_order = "ascending") # high educated hazara > low educated hazara



group <- c("Hazara Candidate", "Non-Hazara Candidate")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Hazara Candidate",
                             "Non-Hazara Candidate"))


figure6 <- ggplot(data = as.data.frame(ethnicity),
                    aes(x = estimate,
                        y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Mean Difference (High - Low Qualifications)", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")


# export to overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b2_hazara.pdf",
       width = 20, height = 13, units = "cm")



# Differences in Marginal Means Among Candidates with Same Qualifications but different ethnicities


ethnicity <- cj(nonhazara, Choice ~ high_educated_reorder,
           id = ~ ID, estimate = "mm_differences",
           by = ~ Hazara,
           level_order = "ascending") 


group <- c("Low Qualified Candidate", "High Qualified Candidate")


ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Low Qualified Candidate",
                             "High Qualified Candidate"))


figure7 <- ggplot(data = as.data.frame(ethnicity),
                    aes(x = estimate,
                        y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Mean Difference (Non-Hazara - Hazara Candidate)", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")



# export to overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b3_hazara.pdf",
       width = 20, height = 13, units = "cm")



# MM estimates across ethnicity and qualifications


ethnicity <- cj(nonhazara, Choice ~ Education_Hazara_Leader,
         id = ~ID,
         estimate= "mm")


# F-Test results with choice to see if F stat sig
f.test.ethnicity <- cj_anova(nonhazara, Choice  ~ Hazara,
                      by = ~ Education_Hazara_Leader
)


ethnicity.rating <- cj(nonhazara, Rating ~ Education_Hazara_Leader,
                id = ~ID,
                estimate= "mm")


# F-Test results reported with rating 
f.test.ethnicity.rating <- cj_anova(nonhazara, Rating  ~ Hazara,
                      by = ~ high_educated
)


group <- c("Hazara Candidate (Higher Educated)", "Non-Hazara Candidate (Higher Educated)",
           "Hazara Candidate (Less Educated)", "Non-Hazara Candidate (Less Educated)")

ethnicity  <- ethnicity %>%
  mutate(group = fct_relevel(group, "Non-Hazara Candidate (Higher Educated)",
                             "Hazara Candidate (Less Educated)",
                             "Non-Hazara Candidate (Less Educated)",
                             "Hazara Candidate (Higher Educated)"))

figure8 <- ggplot(data = as.data.frame(ethnicity),
                 aes(x = estimate,
                     y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")+
  ggtitle("")


# export to overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h1b_hazara.pdf",
       width = 20, height = 13, units = "cm")


### 4. Qualifications and Non-Hazara Men's Support for Out-Group Candidates ####


# Subset analysis to Male Non-Hazara Respondents

nhmale <- nonhazara[nonhazara$Res_Gender == "Male Respondent",]



## Effect of qualifications across gender and ethnicity

nhmale$high_educated_reorder <- relevel(nhmale$high_educated, ref = "Low")


inter <- cj(nhmale, Choice ~ Hazara_Gender_Leader,
           id = ~ ID, estimate = "mm_differences",
           by = ~ high_educated_reorder,
           level_order = "ascending") 


group <- c("Hazara Female Candidate", "Hazara Male Candidate", 
           "Non-Hazara Female Candidate", "Non-Hazara Male Candidate")

inter  <- inter %>%
  mutate(group = fct_relevel(group, "Non-Hazara Female Candidate",
                             "Non-Hazara Male Candidate",
                             "Hazara Female Candidate",
                             "Hazara Male Candidate"))


figure9 <- ggplot(data = as.data.frame(inter),
                    aes(x = estimate,
                        y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Mean Differences (High - Low Qualifications)", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")


# export to overleaf

figure9


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h2a_3_nhmale.pdf",
       width = 20, height = 13, units = "cm")



## MM differences between in-group and out-group while controlling for qualifications


nhmale$Hazara_Gender_Leader_reorder <- ifelse(nhmale$Hazara_Gender_Leader == "Non-Hazara Male Leader", 
                                              "In-Group (Non-Hazara Male)", 
                                              "Out-Group (Other)")


nhmale$Hazara_Gender_Leader_reorder <- as.factor(nhmale$Hazara_Gender_Leader_reorder)

nhmale$Hazara_Gender_Leader_reorder <- relevel(nhmale$Hazara_Gender_Leader_reorder, 
                                               ref = "Out-Group (Other)")


inter <- cj(nhmale, Choice ~ high_educated_reorder,
            id = ~ ID, estimate = "mm_differences",
            by = ~ Hazara_Gender_Leader_reorder,
            level_order = "ascending") # high educated female > low educaated female


group <- c("Low Qualified Candidate", "High Qualified Candidate")

inter  <- inter %>%
  mutate(group = fct_relevel(group, "Low Qualified Candidate", 
                             "High Qualified Candidate"))


figure10 <- ggplot(data = as.data.frame(inter),
                   aes(x = estimate,
                       y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Mean Difference (Non-Hazara Male - Out-Group Candidate)", y = "")+
  geom_vline(xintercept = 0, 
             linetype = "dashed")+
  ggtitle("")


# export to overleaf

ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h2b3_all.pdf",
       width = 20, height = 13, units = "cm")



## Marginal mean estimates across qualifications, gender and ethnicity


inter <- cj(nhmale, Choice ~ Hazara_Education_Gender_Leader,
         id = ~ID,
         estimate= "mm")

# F-tests cited in the text

# F-Test results with choice (Hazara, Gender and Education)
f.test.inter <- cj_anova(nhmale, Choice  ~ Hazara_Gender_Leader,
                      by = ~ Hazara_Education_Gender_Leader
)


inter.rating <- cj(nhmale, Rating ~ Hazara_Education_Gender_Leader,
                id = ~ID,
                estimate= "mm")


# F-Test results  with choice
f.test.inter.rating <- cj_anova(nhmale, Rating  ~ Hazara_Gender_Leader,
                      by = ~ Hazara_Education_Gender_Leader
)


group <- c("Female Candidate (Hazara, Higher Educated)", "Male Candidate (Hazara, Higher Educated)",
           "Female Candidate (Non-Hazara, Higher Educated)", "Male Candidate (Non-Hazara, Higher Educated)",
           "Female Candidate (Hazara, Less Educated)", "Male Candidate (Hazara, Less Educated)",
           "Female Candidate (Non-Hazara, Less Educated)", "Male Candidate (Non-Hazara, Less Educated)")
           
inter  <- inter %>%
  mutate(group = fct_relevel(group, "Male Candidate (Non-Hazara, Higher Educated)",
                             "Female Candidate (Hazara, Less Educated)",
                             "Male Candidate (Non-Hazara, Less Educated)",
                             "Female Candidate (Hazara, Higher Educated)",
                             "Male Candidate (Hazara, Higher Educated)",
                             "Female Candidate (Non-Hazara, Less Educated)",
                             "Male Candidate (Hazara, Less Educated)",
                             "Female Candidate (Non-Hazara, Higher Educated)"))
        
                             
figure11 <- ggplot(data = as.data.frame(inter),
                 aes(x = estimate,
                     y = group)) + 
  geom_point(size = 3)+ 
  geom_pointrange(aes(xmin= estimate - 2*std.error,
                      xmax=estimate + 2*std.error))+
  labs(x="Marginal Means", y = "")+
  geom_vline(xintercept = 0.5, 
             linetype = "dashed")+
  ggtitle("")


# export to overleaf


ggsave("/Users/stephenmonroe/Dropbox/Apps/Overleaf/Qualifications Afghanistan (v2)/plots/h3b_nhmale.pdf",
       width = 20, height = 13, units = "cm")




